#include<sw/redis++/redis++.h>
#include<iostream>
#include<vector>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using sw::redis::Redis;

void test1(Redis& redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key","p1",99);
    redis.zadd("key",{
        std::make_pair("p2",92),
        std::make_pair("p3",78)
    });

    vector<std::pair<string,double>> members = {
        std::make_pair("p4",89),
        std::make_pair("p5",47)
    };
    redis.zadd("key",members.begin(),members.end());

    //带不带score，取决于插入迭代器指向的容器类型
    vector<string> mems;
    auto it = std::back_inserter(mems);
    redis.zrange("key",0,-1,it);
    for(auto e : mems)
        cout << e << endl;

    vector<std::pair<string,double>> results;
    auto it2 = std::back_inserter(results);
    redis.zrange("key",0,-1,it2);
    for(auto e : results)
        cout << e.first << " " << e.second << endl;
}

void test2(Redis& redis)
{
    cout << "zcard" << endl;
    redis.flushall();

    redis.zadd("key","f1",111);
    redis.zadd("key","f2",222);
    redis.zadd("key","f3",333);
    redis.zadd("key","f4",444);

    auto len = redis.zcard("key");
    cout << len << endl;

}

void test3(Redis& redis)
{
    cout << "zrem" << endl;
    redis.flushall();

    redis.zadd("key","f1",111);
    redis.zadd("key","f2",222);
    redis.zadd("key","f3",333);
    redis.zadd("key","f4",444);

    redis.zrem("key","f3");

    long long len = redis.zcard("key");
    cout << len << endl;
}

void test4(Redis& redis)
{
    cout << "zscore" << endl;
    redis.flushall();

    redis.zadd("key","f1",111);
    redis.zadd("key","f2",222);
    redis.zadd("key","f3",333);
    redis.zadd("key","f4",444);

    auto score = redis.zscore("key","f2");
    if(score)
        cout << score.value() << endl;

}

void test5(Redis& redis)
{
    cout << "zrank" << endl;
    redis.flushall();

    redis.zadd("key","f1",111);
    redis.zadd("key","f2",222);
    redis.zadd("key","f3",333);
    redis.zadd("key","f4",444);

    auto rank = redis.zrank("key","f3");
    if(rank)
        cout << rank.value() << endl;
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    //test1(redis);
    //test2(redis);
    //test3(redis);
    //test4(redis);
    test5(redis);

    return 0;
}